#!/bin/bash
# notesettings - script to set defaults for note - a simple note-taking application
# Copyright ©2013-2023 Jason Trunks
# https://notabug.org/JasKinasis/note

# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

NOTESETTINGSVERSION="v1.0"
NOTEDIR="$HOME/.note"
NOTECONFIG="$NOTEDIR/settings.cfg"
NO_ARGS_SO_LIST=0
ED="$EDITOR"          # Use the systems default text editor
KEEPEDITORBACKUPS="0"
PAGER="$(which less)" # typically /usr/bin/less"

if [ ! -d "$NOTEDIR" ]; then
	mkdir -p "$NOTEDIR"
fi

if [ -f "$NOTECONFIG" ]; then
	. "$NOTECONFIG"
fi

if [ $# -eq $NO_ARGS_SO_LIST ]; then
    #list options
    echo "Current settings:"
    echo "ED=$ED"
    echo "KEEPEDITORBACKUPS=$KEEPEDITORBACKUPS"
    echo "PAGER=$PAGER"
    exit 0
else
	while getopts "b:e:p:h" OPTION
	do
		case $OPTION in
			b )
				KEEPEDITORBACKUPS=$OPTARG
				;;
			e )
				if [ -f "$OPTARG" ]; then
					ED=$OPTARG;
			    	fi
				;;
			p )
				if [ -f "$OPTARG" ]; then
					PAGER=$OPTARG;
				fi
				;;
			h )
				echo "notesettings: set user preferences for note";
				echo "Version: $NOTESETTINGSVERSION"
                echo 'Copyright ©2013-2023 Jason Trunks'
                echo 'This program is free software licensed under the GPLV3 and comes with ABSOLUTELY NO WARRANTY.'
                echo
				echo "Usage:"
				echo "notesettings [Options]";
				echo; echo "Available Options:"
				echo; echo "-b {numeric value} - Whether to keep backup files from text editor. (0 to delete backups, 1 to keep them).";
			   	echo "-e {path to editor executable} - Set the editor to use for taking notes."
				echo "-p {path to pager}             - Set the pager for viewing notes."
				echo "-h                             - Display help.";
				echo; echo "Running without passing any parameters will display the current settings.";
				exit 0;
				;;
			* )
				;;
		esac
	done

fi

# Write the settings out to the config file
echo "Current settings:"
echo "ED=$ED" | tee "$NOTECONFIG"
echo "KEEPEDITORBACKUPS=$KEEPEDITORBACKUPS" | tee -a "$NOTECONFIG"
echo "PAGER=$PAGER" | tee -a "$NOTECONFIG"

